home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / contrib / gle.2.1 / demo / joinstyle / joinstyle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  4.3 KB  |  191 lines

  1.  
  2. /* cylinder drawing demo */
  3. /* this demo demonstrates the various join styles
  4.  
  5. /* required include files */
  6. #include <GL/gl.h>
  7. #include "glut.h"
  8. #include "GL/tube.h"
  9.  
  10. /* the arrays in which we will store out polyline */
  11. #define NPTS 6
  12. double points [NPTS][3];
  13. float colors [NPTS][3];
  14. int idx = 0;
  15.  
  16. /* some utilities for filling that array */
  17. #define PNT(x,y,z) {             \
  18.    points[idx][0] = x;             \
  19.    points[idx][1] = y;             \
  20.    points[idx][2] = z;            \
  21.    idx ++;                \
  22. }
  23.  
  24. #define COL(r,g,b) {             \
  25.    colors[idx][0] = r;             \
  26.    colors[idx][1] = g;             \
  27.    colors[idx][2] = b;            \
  28. }
  29.  
  30. /* 
  31.  * Initialize a bent shape with three segments. 
  32.  * The data format is a polyline.
  33.  *
  34.  * NOTE that neither the first, nor the last segment are drawn.
  35.  * The first & last segment serve only to determine that angle 
  36.  * at which the endcaps are drawn.
  37.  */
  38.  
  39. void InitCyl (void) {
  40.  
  41.    COL (0.0, 0.0, 0.0);
  42.    PNT (-6.0, 6.0, 0.0);
  43.  
  44.    COL (0.0, 0.8, 0.3);
  45.    PNT (6.0, 6.0, 0.0);
  46.  
  47.    COL (0.8, 0.3, 0.0);
  48.    PNT (6.0, -6.0, 0.0);
  49.  
  50.    COL (0.2, 0.3, 0.9);
  51.    PNT (-6.0, -6.0, 0.0);
  52.  
  53.    COL (0.2, 0.8, 0.5);
  54.    PNT (-6.0, 6.0, 0.0);
  55.  
  56.    COL (0.0, 0.0, 0.0);
  57.    PNT (6.0, 6.0, 0.0);
  58.  
  59. }
  60.  
  61. float lastx=0;
  62. float lasty=0;
  63.  
  64. /* draw the cylinder shape */
  65. void DrawCyl (void) {
  66.  
  67.    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  68.  
  69.    /* set up some matrices so that the object spins with the mouse */
  70.    glPushMatrix ();
  71.    glTranslatef (0.0, 0.0, -80.0);
  72.    glRotatef (lastx, 0.0, 1.0, 0.0);
  73.    glRotatef (lasty, 1.0, 0.0, 0.0);
  74.  
  75.    /* Phew. FINALLY, Draw the polycylinder  -- */
  76.    glePolyCylinder (NPTS, points, colors, 2.3);
  77.  
  78.    glPopMatrix ();
  79.  
  80.    glutSwapBuffers ();
  81. }
  82.  
  83. /* get notified of mouse motions */
  84. void MouseMotion (int x, int y)
  85. {
  86.    lastx = x;
  87.    lasty = y;
  88.    glutPostRedisplay ();
  89. }
  90.  
  91. void JoinStyle (int msg) 
  92. {
  93.    int style;
  94.    /* get the current joint style */
  95.    style = gleGetJoinStyle ();
  96.  
  97.    /* there are four different join styles, 
  98.     * and two different normal vector styles */
  99.    switch (msg) {
  100.       case 0:
  101.          style &= ~TUBE_JN_MASK;
  102.          style |= TUBE_JN_RAW;
  103.          break;
  104.       case 1:
  105.          style &= ~TUBE_JN_MASK;
  106.          style |= TUBE_JN_ANGLE;
  107.          break;
  108.       case 2:
  109.          style &= ~TUBE_JN_MASK;
  110.          style |= TUBE_JN_CUT;
  111.          break;
  112.       case 3:
  113.          style &= ~TUBE_JN_MASK;
  114.          style |= TUBE_JN_ROUND;
  115.          break;
  116.  
  117.       case 20:
  118.          style &= ~TUBE_NORM_MASK;
  119.          style |= TUBE_NORM_FACET;
  120.          break;
  121.       case 21:
  122.          style &= ~TUBE_NORM_MASK;
  123.          style |= TUBE_NORM_EDGE;
  124.          break;
  125.  
  126.       case 99:
  127.          exit (0);
  128.  
  129.       default:
  130.          break;
  131.    }
  132.    gleSetJoinStyle (style);
  133.    glutPostRedisplay ();
  134. }
  135.  
  136. /* set up a light */
  137. GLfloat lightOnePosition[] = {40.0, 40, 100.0, 0.0};
  138. GLfloat lightOneColor[] = {0.99, 0.99, 0.99, 1.0}; 
  139.  
  140. GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
  141. GLfloat lightTwoColor[] = {0.99, 0.99, 0.99, 1.0}; 
  142.  
  143. main (int argc, char * argv[]) {
  144.  
  145.    /* initialize glut */
  146.    glutInit (&argc, argv);
  147.    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  148.    glutCreateWindow ("cylinder");
  149.    glutDisplayFunc (DrawCyl);
  150.    glutMotionFunc (MouseMotion);
  151.  
  152.    /* create popup menu */
  153.    glutCreateMenu (JoinStyle);
  154.    glutAddMenuEntry ("Raw Join Style", 0);
  155.    glutAddMenuEntry ("Angle Join Style", 1);
  156.    glutAddMenuEntry ("Cut Join Style", 2);
  157.    glutAddMenuEntry ("Round Join Style", 3);
  158.    glutAddMenuEntry ("------------------", 9999);
  159.    glutAddMenuEntry ("Facet Normal Vectors", 20);
  160.    glutAddMenuEntry ("Edge Normal Vectors", 21);
  161.    glutAddMenuEntry ("------------------", 9999);
  162.    glutAddMenuEntry ("Exit", 99);
  163.    glutAttachMenu (GLUT_MIDDLE_BUTTON);
  164.  
  165.    /* initialize GL */
  166.    glClearDepth (1.0);
  167.    glEnable (GL_DEPTH_TEST);
  168.    glClearColor (0.0, 0.0, 0.0, 0.0);
  169.    glShadeModel (GL_SMOOTH);
  170.  
  171.    glMatrixMode (GL_PROJECTION);
  172.    /* roughly, measured in centimeters */
  173.    glFrustum (-9.0, 9.0, -9.0, 9.0, 50.0, 150.0);
  174.    glMatrixMode(GL_MODELVIEW);
  175.  
  176.    /* initialize lighting */
  177.    glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition);
  178.    glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor);
  179.    glEnable (GL_LIGHT0);
  180.    glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition);
  181.    glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor);
  182.    glEnable (GL_LIGHT1);
  183.    glEnable (GL_LIGHTING);
  184.    glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
  185.    glEnable (GL_COLOR_MATERIAL);
  186.  
  187.    InitCyl ();
  188.  
  189.    glutMainLoop ();
  190. }
  191.